<!DOCTYPE html>
<html>
<head>
<base href="${basePath}">
<meta charset="UTF-8">
<title>消费</title> 
</head>
<body>
	$!{include("import_js_css.html")}
	$!{include("header.html")}
	
	<!-- #var(Member member) -->
	
	<!-- #var(PaginatedList<ProductRemaining> prList) -->
	<!--
		#set(
			prPager = prList.pager,
			totalPRResults = prPager.getCountOfTotalResults(),
			totalPRPages = prPager.getCountOfTotalPages() * 1,
			pageSize = prPager.pageSize,
			pagePRNo = prPager.pageNo,
			prePRPageNo = prPager.getPrevPageNo(),
			nextPRPageNo = prPager.getNextPageNo()
		)
	-->
	
	<!-- #var(PaginatedList<Chedule> ccList) -->
	<!--
		#set(
			ccPager = ccList.pager,
			totalCCResults = ccPager.getCountOfTotalResults(),
			totalCCPages = ccPager.getCountOfTotalPages() * 1,
			pageSize = ccPager.pageSize,
			pageCCNo = ccPager.pageNo,
			preCCPageNo = ccPager.getPrevPageNo(),
			nextCCPageNo = ccPager.getNextPageNo()
		)
	-->
	<div class="container menu_all">
		<!-- 搜索栏start -->
		<div class="panel panel-info clear consumption_search panel-border" style="background: white;">
			<form name="consumptionForm" class="form-horizontal" action="yueyogaAdmin/searchOneConsumption.jhtml" method="post">
				<label for="inputCardNumber" class="col-sm-1 control-label">卡号</label>
				<div class="col-sm-3">
					<input name="cardNumber" type="text" class="form-control" id="inputCardNumber" value="${member.cardnumber}">
				</div>
				<label for="inputMemberName" class="col-sm-1 control-label">姓名</label>
				<div class="col-sm-3">
					<input name="memberName" type="text" class="form-control" id="inputMemberName" value="${member.name}">
				</div>
				
				<button id="findButton" type="button" class="btn btn-default btn-ade-default" style="margin-right: 50px;">查询</button>
				
				<input id="pagePRNo" name="pagePRNo" type="hidden" value="${pagePRNo}">
				<input id="pageCCNo" name="pageCCNo" type="hidden" value="${pageCCNo}">
				<!-- 记录当前剩余产品编号 -->
				<input id="pageProductId" name="prId" type="hidden" value="${pageProductId}">
				<!-- 记录课程总共几页 -->
				<input id="pageCCTotalPages" name="totalCCPages" type="hidden" value="${pageTotalCCPages}">
			</form>
		</div>
		<!-- 搜索栏end -->
		
		<!-- #if(resultBool == true) -->
		<!-- 剩余产品列表start -->
		<div class="panel panel-info clear main_info panel-border" style="background: white;">
			<div class="panel-heading search-result-title">
				<h3 class="panel-title">剩余产品</h3>
			</div>
			
			<!-- #if(prList.size > 0) -->
			<table id="task-list" class="table table-bordered table-hover table-condensed tb" style="text-align: center;">
				<tr class="tb-tr-tit">
					<td style="text-align: center;" width="5%"></td>
					<td style="width:90px;">产品编号</td>
					<td>产品名称</td>
					<td style="width:160px;">产品类型</td>
					<td class="tb-teacher">可使用次数</td>
					<td class="tb-teacher">价格(元)</td>
					<td class="tb-time">起始时间</td>
					<td class="tb-time">截止时间</td>
				<tr>
				
				<!-- #for(ProductRemaining pr : prList) -->
					<tr>
						<td style="text-align: center;" width="5%">
							<!-- #if(pageProductId != null) -->
								<!-- #if(pageProductId.toInt == pr.id) -->
									<input name="pr_checkbox_${pr.id}" checked type="checkbox">
								<!-- #else -->
									<input name="pr_checkbox_${pr.id}" type="checkbox">
								<!-- #end -->
							<!-- #else -->
								<!-- #if(for.index == 0) -->
									<input name="pr_checkbox_${pr.id}" checked type="checkbox">
								<!-- #else -->
									<input name="pr_checkbox_${pr.id}" type="checkbox">
								<!-- #end -->
							<!-- #end -->
						</td>
						<td>${pr.product.productId}</td>
						<td>${pr.product.name}</td>
						<td>
							<!-- #if(pr.product.type.toInt == 101) -->
								次卡
							<!-- #else(pr.product.type.toInt == 102) -->
								时间卡
							<!-- #else(pr.product.type.toInt == 107) -->
								金额卡
							<!-- #else(pr.product.type.toInt == 103) -->
								年卡
							<!-- #else(pr.product.type.toInt ==104) -->
								季卡
							<!-- #else(pr.product.type.toInt == 105) -->
								月卡
							<!-- #else(pr.product.type.toInt == 106) -->
								半月卡
							<!-- #else -->
								普通消费
							 <!-- #end -->
						</td>
						<td>${pr.counts}</td>
						<td>
							<!-- #if(pr.product.type.toInt == 107) -->
								${pr.product.price}/次
							<!-- #else(pr.product.type.toInt == 108) -->
								${pr.product.price}/次
							<!-- #else -->
								${pr.product.price}
							<!-- #end -->
						</td>
						<td>${pr.beginTime.format("yyyy-MM-dd")}</td>
						<td>${pr.endTime.format("yyyy-MM-dd")}</td>
					</tr>
				<!--#end-->
			</table>
			<div class="div_page main_div_page">
				<ul class="pagination">
					<li><a href="javascript:void(0)" onclick="changePRPage(1)">首页</a></li>
					<!-- #if(pagePRNo==1) -->
						<li class="disabled"><a href="javascript:void(0)">上一页</a></li>
					<!-- #else -->
						<li><a href="javascript:void(0)" onclick="changePRPage(${prePRPageNo})">上一页</a></li>
					<!-- #end -->
					
					<!-- #if(pagePRNo==totalPRPages) -->
						<li class="disabled"><a href="javascript:void(0)">下一页</a></li>
					<!-- #else -->
						<li><a href="javascript:void(0)" onclick="changePRPage(${nextPRPageNo})">下一页</a></li>
					<!-- #end -->
					
					<li><a href="javascript:void(0)" onclick="changePRPage(${totalPRPages})">尾页</a></li>
					<li>
						<a>第<input id="txtPRPageNumber" value="${pagePRNo}" onkeyup="this.value=this.value.replace(/\D/g,'')" onblur="changePRPage(this.value)" type="text" />页&nbsp;~&nbsp;共  ${totalPRPages}&nbsp;页/${totalPRResults}条</a>
					</li>
				</ul>
			</div>
			<!-- #else -->
				未找到可用产品
			<!-- #end -->
		</div>
		<!-- 剩余产品列表end -->

		<!-- 班级课时列表start -->
		<div class="panel panel-info main_info panel-border" style="background: white;">
			<div class="panel-heading search-result-title">
				<h3 class="panel-title">班级课时</h3>
				<div class="btn-group btn-gp-default" style="margin-left: 10px !important;">
					<button id="addButton" type="button" class="btn btn-default panel-radius-left-right">消费</button>
				</div>
			</div>
			
			<table id="schedule-list" class="table table-bordered table-hover table-condensed tb" style="text-align: center;">
				<tr class="tb-tr-tit">
					<td></td>
					<td>班级编号</td>
					<td class="tb-teacher">班级名称</td>
					<td>课程起始时间</td>
					<td>课程结束时间</td>
					<td class="tb-teacher">教练</td>
					<td class="tb-teacher">助教</td>
					<td class="tb-teacher">场地</td>
					<td class="tb-time">周几</td>
					<td class="tb-teacher">班级类型</td>
				<tr>

				<!-- #set(boolean flag = true) -->
				<!--#for(Chedule schedule : ccList)-->
					<tr>
						<td style="text-align: center;">
							<!-- #if(schedule.expired == 1) -->
								
							<!-- #else -->
								<!-- #if(flag) -->
									<input name="cc_checkbox_${schedule.id}_${schedule.isReservation}" checked type="checkbox">
									<!-- #set(flag = false) -->
								<!-- #else -->
									<input name="cc_checkbox_${schedule.id}_${schedule.isReservation}" type="checkbox">
								<!-- #end -->
							<!-- #end -->
						</td>
						<td>${schedule.clazz.id}</td>
						<td>${schedule.clazz.name}</td>
						<td>${schedule.starttime}</td>
						<td>${schedule.endtime}</td>
						<td>${schedule.teacher.name}</td>
						<td>${schedule.assistant.name}</td>
						<td>${schedule.place.name}</td>
						<td>
							<!-- #if(schedule.week.toInt() == 1) -->
								周一
							<!-- #else(schedule.week.toInt() == 2) -->
								周二
							<!-- #else(schedule.week.toInt() == 3) -->
								周三
							<!-- #else(schedule.week.toInt() == 4) -->
								周四
							<!-- #else(schedule.week.toInt() == 5) -->
								周五
							<!-- #else(schedule.week.toInt() == 6) -->
								周六
							<!-- #else(schedule.week.toInt() == 7) -->
								周日
							<!-- #end -->
						</td>
						<td>${schedule.clazz.type}</td>
					</tr>
				<!--#end-->
			</table>
			<div id="cc_page" class="div_page main_div_page">
				<ul class="pagination">
					<li><a href="javascript:void(0)" onclick="changeCCPage(1)">首页</a></li>
					<!-- #if(pageCCNo==1) -->
						<li id="cc_pageUp" class="disabled"><a href="javascript:void(0)">上一页</a></li>
					<!-- #else -->
						<li id="cc_pageUp"><a href="javascript:void(0)" onclick="changeCCPage(${preCCPageNo})">上一页</a></li>
					<!-- #end -->
					
					<!-- #if(pageCCNo==totalCCPages) -->
						<li id="cc_pageDown" class="disabled"><a href="javascript:void(0)">下一页</a></li>
					<!-- #else -->
						<li id="cc_pageDown"><a href="javascript:void(0)" onclick="changeCCPage(${nextCCPageNo})">下一页</a></li>
					<!-- #end -->
					
					<li><a href="javascript:void(0)" onclick="changeCCPage(${totalCCPages})">尾页</a></li>
					<li>
						<a>第<input id="txtCCPageNumber" value="${pageCCNo}" onkeyup="this.value=this.value.replace(/\D/g,'')" onblur="changeCCPage(this.value)" type="text" />页&nbsp;~&nbsp;共 <span id="cc_totalCCPages">${totalCCPages}</span>&nbsp;页/<span id="cc_totalCCResults">${totalCCResults}</span>条</a>
					</li>
				</ul>
			</div>
		</div>
		<!-- 班级课时列表end -->
		<!-- #else -->
			<!-- #if(resultBool == null) -->
			<!-- #else -->
				<div class="panel panel-info clear main_info panel-border" style="background: white; text-align: center; line-height: 100px;">
					<div class="panel-heading search-result-title">
						<h3 class="panel-title">提示</h3>
					</div>
					${result}
				</div>
			<!-- #end -->
		<!-- #end -->
	</div>
	
	<script type="text/javascript">
		$(document).ready(function() {
			//卡号文本框回车事件
			$("input[name='cardNumber']").keyup(function(event) {
				if (event.keyCode == 13) {
					window.consumptionForm.submit();
				}
			});
			
			//点击剩余产品复选框查询课程
			$("input[name^='pr_checkbox_']").click(function() {
				if (!$(this).prop("checked")) {
					return;
				}
				$("input[name^='pr_checkbox_']").prop("checked", false);
				$(this).prop("checked", true);
				//
				var name = $(this).attr("name");
				name = name.substring(12, name.length);
				
				if (name == null || name.length <= 0) {
					error("请选择产品编号！");
					return;
				}
				
				$.ajax({
					type		: "post",
					url			: "${basePath}/yueyogaAdmin/findConsumptionWhenClickCheckbox.jhtml",
					async		: false,
					data		: {"productId": name},
					success		: function(response) {
						var res = eval("(" + response + ")");
						if (res.success) {
							$("#schedule-list").html("<tr class='tb-tr-tit'><td></td><td>班级编号</td><td class='tb-teacher'>班级名称</td><td>课程起始时间</td><td>课程结束时间</td><td class='tb-teacher'>教练</td><td class='tb-teacher'>助教</td><td class='tb-teacher'>场地</td><td class='tb-time'>周几</td><td class='tb-teacher'>班级类型</td><tr>");
							var expired = 0;
							var ap = "";
							var flag = true;
							for (var i = 0;i < res.items.length;i ++) {
								expired = res.items[i].expired * 1;
								if (expired == 1) {
									ap = "<td></td>";
								} else {
									if (flag) {
										ap = "<td><input name='cc_checkbox_" + res.items[i].scheduleId + "_" + res.items[i].isReservation + "' type='checkbox' checked></td>";
										flag = false;
									} else {
										ap = "<td><input name='cc_checkbox_" + res.items[i].scheduleId + "_" + res.items[i].isReservation +  "' type='checkbox'></td>";
									}
								}
								$("#schedule-list").append(
									"<tr>" + ap +
									"<td>" + res.items[i].classId + "</td>" + 
									"<td>" + res.items[i].className + "</td>" + 
									"<td>" + res.items[i].scheduleStarttime + "</td>" + 
									"<td>" + res.items[i].scheduleEndtime + "</td>" + 
									"<td>" + res.items[i].coachName + "</td>" + 
									"<td>" + res.items[i].assistantName + "</td>" + 
									"<td>" + res.items[i].placeName + "</td>" + 
									"<td>" + res.items[i].week + "</td>" + 
									"<td>" + res.items[i].classType + "</td></tr>"
								);
							}
							
							$("#cc_totalCCPages").text(res.totalPages);
							$("#cc_totalCCResults").text(res.totalResults);
							$("#pageProductId").val(name);
							$("#pageCCTotalPages").val(res.totalPages);
							$("#txtCCPageNumber").val(1);
							
							var totalPages = res.totalPages * 1;
							var page = $("#pageCCNo").val();
							page = page * 1;
							
							if (totalPages <= 1) {
								$("#cc_page ul li:eq(1)").addClass("disabled");
								$("#cc_page ul li:eq(1)").html("<a href='javascript:void(0)'>上一页</a>");
								$("#cc_page ul li:eq(2)").addClass("disabled");
								$("#cc_page ul li:eq(2)").html("<a href='javascript:void(0)'>下一页</a>");
							} else {
								if (page >= totalPages) {
									$("#cc_page ul li:eq(1)").addClass("disabled");
									$("#cc_page ul li:eq(1)").html("<a href='javascript:void(0)'>上一页</a>");
									$("#cc_page ul li:eq(2)").addClass("disabled");
									$("#cc_page ul li:eq(2)").html("<a href='javascript:void(0)'>下一页</a>");
								} else if (page <= 1) {
									$("#cc_page ul li:eq(1)").addClass("disabled");
									$("#cc_page ul li:eq(1)").html("<a href='javascript:void(0)'>上一页</a>");
									$("#cc_page ul li:eq(2)").removeClass("disabled");
									var nextPage = page + 1;
									$("#cc_page ul li:eq(2)").html("<a href='javascript:void(0)' onclick='changeCCPage(" + nextPage + ")'>下一页</a>");
								} else {
									$("#cc_page ul li:eq(1)").removeClass("disabled");
									var prePage = page - 1;
									$("#cc_page ul li:eq(1)").html("<a href='javascript:void(0)' onclick='changeCCPage(" + prePage + ")'>上一页</a>");
									$("#cc_page ul li:eq(2)").removeClass("disabled");
									var nextPage = page + 1;
									$("#cc_page ul li:eq(2)").html("<a href='javascript:void(0)' onclick='changeCCPage(" + nextPage + ")'>下一页</a>");
								}
							}
							
							$("input[name^='cc_checkbox_']").click(function() {
								if ($(this).prop("checked")) {
									$("input[name^='cc_checkbox_']").prop("checked", false);
									$(this).prop("checked", true);
								}
							});
						} else {
							$("#schedule-list").html("<tr class='tb-tr-tit'><td></td><td>班级编号</td><td class='tb-teacher'>班级名称</td><td>课程起始时间</td><td>课程结束时间</td><td class='tb-teacher'>教练</td><td class='tb-teacher'>助教</td><td class='tb-teacher'>场地</td><td class='tb-time'>周几</td><tr>");
							$("#txtCCPageNumber").val(1);
							$("#cc_totalCCPages").text(0);
							$("#cc_totalCCResults").text(0);
							error(res.msg);
						}
					}
				});
			});
			
			//查询
			$("#findButton").click(function() {
				window.consumptionForm.submit();
			});
			
			//单选课程
			$("input[name^='cc_checkbox_']").click(function() {
				if ($(this).prop("checked")) {
					$("input[name^='cc_checkbox_']").prop("checked", false);
					$(this).prop("checked", true);
				}
			});
			
			//保存消费记录
			$("#addButton").click(function() {
				var memberId = "${member.memberid}";
				var memberName = "${member.name}";
				
				//判断选择的剩余产品个数是否大于一个
				var lenPR = $("input[name^='pr_checkbox_']:checkbox:checked").length;
				if (lenPR > 1) {
					error("剩余产品只能选择一个！");
					return;
				} else if (lenPR < 1) {
					error("请选择一个产品！");
					return;
				}
				//获得剩余产品编号
				var prName = $("input[name^='pr_checkbox_']:checkbox:checked").attr("name");
				var prId = prName.substring(12, prName.length);
				
				//判断选择的班级个数是否大于一个
				var lenCC = $("input[name^='cc_checkbox_']:checkbox:checked").length;
				if (lenCC > 1) {
					error("班级课程只能选择一个！");
					return;
				} else if (lenCC < 1) {
					error("请选择一个班级课程！");
					return;
				}
				//获得班级课程编号
				var ccName = $("input[name^='cc_checkbox_']:checkbox:checked").attr("name");
				var cc = ccName.split("_");
				var ccId = cc[2];
				
				//获得新旧课程标识
				var isReservation = cc[3];
				
				/* //获得班级编号
				var clazzId = $("td[id='cc_clazz_id_" + ccId + "']").text();
				
				//获得班级名称
				var clazzName = $("td[id='cc_clazz_name_" + ccId + "']").text();
				
				//获得班级课程开始时间
				var scheduleStartTime = $("td[id='cc_starttime_" + ccId + "']").text();
				
				//获得班级课程结束时间
				var scheduleEndTime = $("td[id='cc_endtime_" + ccId + "']").text();
				
				//获得班级课程教练编号
				var coachId = $("input[name='cc_teacher_id_" + ccId + "']").val();
				
				//获得班级课程教练姓名
				var coachName = $("td[id='cc_teacher_" + ccId + "']").text().trim();
				
				//获得班级课程助教编号
				var assistantId = $("input[name='cc_assistant_id_" + ccId + "']").val();
				
				//获得班级课程助教姓名
				var assistantName = $("td[id='cc_assistant_" + ccId + "']").text().trim();
				
				//获得班级课程场地编号
				var placeId = $("input[name='cc_place_id_" + ccId + "']").val();
				
				//获得班级课程场地姓名
				var placeName = $("td[id='cc_place_" + ccId + "']").text().trim();
				
				//获得周几
				var week = $("input[name='cc_week_" + ccId + "']").val(); 
				
				
				$.post(
					"yueyogaAdmin/addConsume.jhtml",
					{"memberId":memberId, "member.name":memberName, "clazzSelectedId":clazzId, "clazzSelectedName":clazzName, "scheduleSelectedId":ccId, "scheduleSelectedStartTime":scheduleStartTime, "scheduleSelectedEndTime":scheduleEndTime, "scheduleSelectedTeacherId":coachId, "scheduleSelectedTeacherName":coachName, "scheduleSelectedAssistantId":assistantId, "scheduleSeletedAssistantName":assistantName, "scheduleSelectedPlaceId":placeId, "scheduleSelectedPlaceName":placeName, "productSelectedRemainingId":prId, "scheduleSelectedWeek":week},
					function(response) {
						var res = eval("(" + response + ")");
						if (res.success) {
							var d = alert("消费成功!");
							setTimeout(function(){
								d.close().remove();
							},1000);
							window.location.href = "${basePath}/yueyogaAdmin/searchProducts.jhtml?index=consumeList&memberId=" + memberId;
						} else {
							alert(res.msg);
						}
					}
				);*/
				
				$.ajax({
					type		: "post",
					url			: "${basePath}/yueyogaAdmin/addConsume.jhtml",
					async		: false,
					data		: {"memberId": memberId, "prId": prId, "scheduleId": ccId, "isReservation": isReservation},
					success		: function(response) {
						var res = eval("(" + response + ")");
						if (res.success) {
							var d = alert(res.msg);
							setTimeout(function() {
								d.close().remove();
							}, 2000);
							
							window.location.href = "${basePath}/yueyogaAdmin/searchOneConsumption.jhtml";
						} else {
							error(res.msg);
						}
					}
				});
			});
			
			//卡号获得光标
			$("input[name='cardNumber']").focus();
			
			$("input[name='cardNumber']").bind("input propertychange", function() {
				$("#pageProductId").val("");
				$("#pageCCTotalPages").val("");
			});
		});
		
		//剩余产品翻页
		function changePRPage(value) {
			value = value * 1;
			var totalPages = '${totalPRPages}';
			if (value > totalPages) {
				value = totalPages;
			} else if (value < 1) {
				value = 1;
			}
			$("#txtPRPageNumber").val(value);
			goToPRPage(value);
		}
		
		function goToPRPage(pageNo) {
			$("#pagePRNo").val(pageNo);
			document.consumptionForm.submit();
		}
		
		//班级课程翻页
		function changeCCPage(value) {
			value = value * 1;
			var totalPages = $("#pageCCTotalPages").val();
			totalPages = totalPages * 1;
			if (value > totalPages) {
				value = totalPages;
			} else if (value < 1) {
				value = 1;
			}
			$("#txtCCPageNumber").val(value);
			goToCCPage(value);
		}
		
		function goToCCPage(pageNo) {
			$("#pageCCNo").val(pageNo);
			document.consumptionForm.submit();
		}
	</script>
</body>
</html>